from editor_base import EditorBase

from ..bases import widgets


class CategoryEditor(EditorBase):
    topwidget_label = 'Categories'
    def init_editor(self, **kwargs):
        self.src_object = kwargs.get('src_object')
        self.category_type = None
        self.category_dict = self.DeviceSystem.Categories
        if 'attribute' in self.src_object.saved_class_name.lower():
            self.category_type = 'attribute'
            self.category_dict = self.DeviceSystem.Categories['attribute']
        elif 'device' in self.src_object.saved_class_name.lower():
            self.category_type = 'device'
            self.category_dict = self.DeviceSystem.Categories['device']
            
        self.list = widgets.TreeList(name='Categories', list_types=[str, str, bool, bool], 
                                     column_names=['id', 'Name', 'Enabled', 'cell_active'], 
                                     column_order=[1, 2], columns_editable=[1])
        self.update_list()
        self.list.connect('cell_toggled', self.on_cell_toggled)
        self.list.connect('cell_edited', self.on_cell_edited)
        self.btn_add_category = widgets.Button(label='Add Category')
        self.btn_add_category.connect('clicked', self.on_btn_add_category_clicked)
        self.list.topwidget.pack_start(self.btn_add_category)
        self.topwidget = self.list.topwidget
        
        self.DeviceSystem.bind(new_category=self.on_new_category)
        if self.src_object is not None:
            self.src_object.bind(category_update=self.on_category_update)
        
    def unlink(self):
        super(CategoryEditor, self).unlink()
        if self.src_object is not None:
            self.src_object.unbind(self.on_category_update)
            self.DeviceSystem.unbind(self.on_new_category)
            
    def update_list(self, key=None):
        if key:
            category = self.category_dict.get(key)
            self.list.update({key:[category.name, self.src_object in category.members, True]})
        else:
            for key, val in self.category_dict.iteritems():
                self.list.update({key:[val.name, self.src_object in val.members, True]})
        
    def on_btn_add_category_clicked(self, btn):
        self.DeviceSystem.add_category(type=self.category_type, name='New Category')
        
    def on_category_update(self, **kwargs):
        category = kwargs.get('category')
        state = kwargs.get('state')
        self.update_list(category.id)
        
    def on_new_category(self, **kwargs):
        category = kwargs.get('category')
        if kwargs.get('type') == self.category_type:
            self.update_list(category.id)
        
    def on_cell_toggled(self, **kwargs):
        key = kwargs.get('key')
        state = kwargs.get('state')
        category = self.category_dict.get(key)
        if state:
            self.src_object.add_category(category)
        else:
            self.src_object.remove_category(category)
            
    def on_cell_edited(self, **kwargs):
        key = kwargs.get('key')
        column = kwargs.get('column')
        value = kwargs.get('value')
        if column == 1:
            self.category_dict[key].name = value
            self.update_list(key)
            
